﻿using AMInstantMessagingServerDao;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UDPHelper;

namespace AMInstantMessagingServerAPI
{
    public static class UserTools
    {
        /// <summary>
        /// 根据消息包获取UserId
        /// </summary>
        /// <param name="package"></param>
        /// <returns></returns>
        public static long GetUserId(UDPPackage package)
        {
            var user_token = GetUserToken(package);
            if (string.IsNullOrEmpty(user_token))
                return 0;

            UserService user_service = new UserService();
            var client_info = user_service.GetClientInfoByToken(user_token);

            if (client_info == null)
                return 0;

            return client_info.UserId;
        }

        /// <summary>
        /// 获取用户小票
        /// </summary>
        /// <param name="package"></param>
        /// <returns></returns>
        public static string GetUserToken(UDPPackage package)
        {
            return UDPPackageHelper.GetHeader(package, "UserToken");
        }
    }
}
